<template>
  <header class="header">
    <h1>todos</h1>
    <input
      id="toggle-all"
      @click="allChecked"
      :disabled="list.length == 0"
      :checked="isAll"
      class="toggle-all"
      type="checkbox"
    />
    <label for="toggle-all"></label>
    <input
      @keydown.enter="add"
      v-model.trim="taskname"
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
    />
  </header>
</template>

<script>
// 导入辅助函数
import { mapState, mapMutations } from 'vuex'

export default {
  data () {
    return {
      taskname: ''
    }
  },
  methods: {
    // 映射全选控制小选 和 添加功能方法
    ...mapMutations(['isAllChecked', 'addTask']),
    allChecked () {
      this.isAllChecked(this.isAll)
    },
    add () {
      // 非空判断
      if (this.taskname.length == 0) return alert('输入不能为空')

      // 添加方法
      this.addTask(this.taskname)

      // 添加完成后清空文本框
      this.taskname = ''
    }
  },
  computed: {
    ...mapState(['list', 'isAll'])
  }
}
</script>
